import dayjs from 'dayjs'

interface formatterDataType {
  text: (val: string) => string
  date: (val: string) => string
  dateTime: (val: string) => string
  number: (val: number | bigint | string, precision: number) => string
  money: (val: number | bigint | string, precision: number) => string
  percent: (val: number | string, precision: number) => string
}

export type formatterType = keyof formatterDataType

class FormatterData implements formatterDataType {
  text(val: string) {
    return val
  }

  date(val: string) {
    return dayjs(val).format('YYYY-MM-DD')
  }

  dateTime(val: string) {
    return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
  }

  number(val: number | bigint | string, precision = 2) {
    return new Intl.NumberFormat('zh-CN', {
      style: 'decimal',
      maximumFractionDigits: precision
    }).format(val as number | bigint)
  }

  money(val: number | bigint | string, precision = 2) {
    return new Intl.NumberFormat('zh-CN', {
      style: 'currency',
      currency: 'CNY',
      maximumFractionDigits: precision,
      minimumFractionDigits: precision
    }).format(val as number | bigint)
  }

  percent(val: number | string, precision = 2) {
    return new Intl.NumberFormat('zh-CN', {
      style: 'percent',
      maximumFractionDigits: precision,
      minimumFractionDigits: precision
    }).format(val as number)
  }
}

export default new FormatterData()
